import request from '@/utils/request'

//https://hq.sinajs.cn/list=M0,A0
export function getSinaQuote(symbol) {
	return request({
		url: '/ajax/get-html',
		method: 'post',
        data: {
            url: 'https://hq.sinajs.cn/rn='+Math.round(Math.random() * 60466176).toString(36)+'&list='+symbol,
            header: [
                {key: 'Content-Type', val: 'application/javascript; charset=GB18030'},
                {key: 'Referer', val: 'https://finance.sina.com.cn'},
                {key: 'User-Agent', val: 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/97.0.4692.71 Safari/537.36 Edg/97.0.1072.55'},
                // {key: 'Accept', val: '*/*'},
                // {key: 'Accept-Encoding', val: 'gzip, deflate, br'},
                // {key: 'Content-Encoding', val: 'gzip'},
            ]
        }
	})
}

//http://stock2.finance.sina.com.cn/futures/api/json.php/IndexService.getInnerFuturesDailyKLine?symbol=M0
export function getSinaFuturesDailyKLine(symbol) {
	return request({
		url: '/ajax/do-get',
		method: 'post',
        data: {
            url: 'https://stock2.finance.sina.com.cn/futures/api/json.php/IndexService.getInnerFuturesDailyKLine?symbol='+symbol,
            header: [
                {key: 'Content-Type', val: 'application/json'},
                {key: 'Referer', val: 'https://finance.sina.com.cn/'},
                {key: 'User-Agent', val: 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/97.0.4692.71 Safari/537.36 Edg/97.0.1072.55'},
                // {key: 'cookie', val: getXueqiuCookieByCache()}
            ]
        }
	})
}

//从sina的js接口网页中，解析JSON数据
export function praseSinaJsStr(html) {
    let _data = html
    _data = _data.replace(/\t/g, '')
    _data = _data.replace(/\n/g, '')
    _data = _data.replace(/\r/g, '')
    _data = _data.replace(/"/g, '')
    _data = _data.replace(/var /g, '')
    _data = _data.replace(/hq_str_/g, '')

    let _ret = {}
    let _res = _data.split(';')
    if (_res.length > 0) {
        _res.forEach(row => {
            let _rowData = row.split('=')
            if (_rowData.length == 2) {
                _ret[_rowData[0]] = _rowData[1].split(',')
            }
        })
    }

    return _ret
}

/**
 * IC: http://vip.stock.finance.sina.com.cn/quotes_service/api/json_v2.php/Market_Center.getNameList?page=1&num=40&sort=symbol&asc=1&node=zzgz_qh&_s_r_a=init
 * IH: http://vip.stock.finance.sina.com.cn/quotes_service/api/json_v2.php/Market_Center.getNameList?page=1&num=40&sort=symbol&asc=1&node=szgz_qh&_s_r_a=init
 * IF: http://vip.stock.finance.sina.com.cn/quotes_service/api/json_v2.php/Market_Center.getNameList?page=1&num=40&sort=symbol&asc=1&node=qz_qh&_s_r_a=init
 */
 export function getSinaInxFuturesSymbol(symbol) {
    let _symbolList = {
        IC: 'zzgz_qh',
        IF: 'qz_qh',
        IH: 'szgz_qh'
    }
	return request({
		url: '/ajax/do-get',
		method: 'post',
        data: {
            url: 'https://vip.stock.finance.sina.com.cn/quotes_service/api/json_v2.php/Market_Center.getNameList?page=1&num=40&sort=symbol&asc=1&node='+_symbolList[symbol]+'&_s_r_a=init',
            header: [
                {key: 'Content-Type', val: 'application/json'},
                {key: 'Referer', val: 'http://vip.stock.finance.sina.com.cn'},
                {key: 'User-Agent', val: 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/97.0.4692.71 Safari/537.36 Edg/97.0.1072.55'},
                // {key: 'cookie', val: getXueqiuCookieByCache()}
            ]
        }
	})
}
